package com.leetcode.offer.chapter2;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Dennis Li
 * @date 2020/7/11 19:52
 */
public class CQueue {

    // 用一个双端队列来代替传统的Stack栈，效率更高
    Deque<Integer> stack1;
    Deque<Integer> stack2;

    public CQueue() {
        stack1 = new LinkedList<>();
        stack2 = new LinkedList<>();
    }

    public void appendTail(int value) {
        stack1.push(value);
    }

    public int deleteHead() {
        // stack2里面是正序的，并且压入栈后就不需要再把Stack2里面的倒回去
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        if (stack2.isEmpty()) {
            return -1;
        } else {
            // stack2的队首即为正确的队首
            return stack2.pop();
        }
    }
}
